package com.crm.business.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.crm.business.crm.mapper.CrmReceivablesDataMapper;
import com.crm.business.crm.mapper.CrmRefundDataMapper;
import com.crm.business.crm.service.ICrmReceivablesDataService;
import com.crm.business.crm.service.ICrmRefundDataService;
import com.crm.business.servlet.BaseServiceImpl;
import com.crm.common.crm.common.CrmModel;
import com.crm.common.crm.entity.PO.CrmReceivablesData;
import com.crm.common.crm.entity.PO.CrmRefundData;
import com.crm.common.crm.entity.VO.CrmModelFiledVO;
import com.crm.common.ka.core.common.Const;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 回款自定义字段存值表 服务实现类
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-05-28
 */
@Service
public class CrmRefundDataServiceImpl extends BaseServiceImpl<CrmRefundDataMapper, CrmRefundData> implements ICrmRefundDataService {

    /**
     * 设置用户数据
     *
     * @param crmModel crmModel
     */
    @Override
    public void setDataByBatchId(CrmModel crmModel) {
        List<CrmRefundData> refundDataList = query().eq("batch_id", crmModel.getBatchId()).list();
        refundDataList.forEach(obj -> {
            crmModel.put(obj.getName(), obj.getValue());
        });
    }

    /**
     * 保存自定义字段数据
     *
     * @param array data
     */
    @Override
    public void saveData(List<CrmModelFiledVO> array, String batchId) {
        List<CrmRefundData> refundDataList = new ArrayList<>();
        remove(new LambdaQueryWrapper<CrmRefundData>().eq(CrmRefundData::getBatchId, batchId));
        Date date = new Date();
        for (CrmModelFiledVO obj : array) {
            CrmRefundData refundData = BeanUtil.copyProperties(obj, CrmRefundData.class);
            refundData.setName(obj.getFieldName());
            refundData.setCreateTime(date);
            refundData.setBatchId(batchId);
            refundDataList.add(refundData);
        }
        saveBatch(refundDataList, Const.BATCH_SAVE_SIZE);
    }

    /**
     * 保存自定义字段数据
     *
     * @param array data
     */
    @Override
    public void saveCheckOkEditData(List<CrmModelFiledVO> array, String batchId) {
        if(!CollectionUtil.isEmpty(array)){
            List<CrmRefundData> refundDataList = new ArrayList<>();
            List<String> fieldName = array.stream().map(CrmModelFiledVO::getFieldName).collect(Collectors.toList());
            remove(new LambdaQueryWrapper<CrmRefundData>().eq(CrmRefundData::getBatchId, batchId).in(CrmRefundData::getName, fieldName));
            Date date = new Date();
            for (CrmModelFiledVO obj : array) {
                CrmRefundData refundData = BeanUtil.copyProperties(obj, CrmRefundData.class);
                refundData.setName(obj.getFieldName());
                refundData.setCreateTime(date);
                refundData.setBatchId(batchId);
                refundDataList.add(refundData);
            }
            saveBatch(refundDataList, Const.BATCH_SAVE_SIZE);
        }
    }

    /**
     * 通过batchId删除数据
     *
     * @param batchId data
     */
    @Override
    public void deleteByBatchId(List<String> batchId) {
        LambdaQueryWrapper<CrmRefundData> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CrmRefundData::getBatchId,batchId);
        remove(wrapper);
    }
}
